package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.exception.apiException.OrderException;
import com.markerhub.handler.Wx_Token_Auth;
import com.markerhub.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author by MaXiaofeng
 * @Description 外卖柜相关操作
 * @Date 2022/2/16 21:10
 * @Version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 外卖员通过用户手机号进行存餐业务，形成订单
     *
     * @param jsonBody
     * @return
     */
    @Wx_Token_Auth
    @PostMapping("makeOrder")
    public Result makeOrder(@RequestBody Map<String, String> jsonBody) throws OrderException {
        //authorization：外卖员对应的微信信息的token来获取其身份内容
        // 参数1，外卖下单的外卖柜Id
        // 参数2：外卖下单手机号码
        int cabinet_id = Integer.parseInt(jsonBody.get("cabinet_id"));
        String phone = jsonBody.get("phone");
        Result result = orderService.makeOrder(cabinet_id, phone);
        //绑定日志
        log.info("{}", result);
        return result;
    }

    /**
     * 获取到哪个柜门关闭，外卖员关闭柜门
     *
     * @param jsonBody
     * @return
     */
    @Wx_Token_Auth
    @PostMapping("closeCabinetByMan")
    public Result closeCabinetByMan(@RequestBody Map<String, String> jsonBody) {
        int cabinet_id = Integer.parseInt(jsonBody.get("cabinet_id"));
        int cabinet_doorNum = Integer.parseInt(jsonBody.get("cabinet_doorNum"));
        Result result = orderService.closeCabinetByMan(cabinet_id, cabinet_doorNum);
        log.info("{}", result);
        return result;
    }

    /**
     * 顾客输入手机验证码，打开柜门
     *
     * @param jsonBody
     * @return
     */
    @Wx_Token_Auth
    @PostMapping("takeFoodByCustomer")
    public Result takeFoodByCustomer(@RequestBody Map<String, String> jsonBody) {
        String phone = jsonBody.get("phone");
        if (phone == null) return Result.fail("上传的手机号不能为空");
        Result result = orderService.takeFoodByCustomer(phone);
        log.info("{}", result);
        return result;
    }

    /**
     * 检测顾客关闭外卖柜柜门，更改订单状态，订单结束
     *
     * @param jsonBody
     * @return
     */
    @Wx_Token_Auth
    @PostMapping("closeDoorByCustomer")
    public Result closeDoorByCustomer(@RequestBody Map<String, String> jsonBody) {
        String cabinetId = jsonBody.get("cabinet_id");
        String doorNum = jsonBody.get("cabinet_doorNum");
        if (cabinetId == null || doorNum == null) return Result.fail("参数不能为空");
        int cabinet_id = Integer.parseInt(cabinetId);
        int cabinet_doorNum = Integer.parseInt(doorNum);

        Result result = orderService.closeDoorByCustomer(cabinet_id, cabinet_doorNum);
        log.info("{}", result);
        return result;
    }

    /**
     * 查询某用户以往订单
     *
     * @param jsonBody
     * @return
     */
    @Wx_Token_Auth
    @PostMapping("historyOrder")
    public Result myHistoryOrder(@RequestBody Map<String, String> jsonBody) {
        int userId = Integer.parseInt(jsonBody.get("userId"));
        Result result = orderService.getHistoryOrder(userId);
        log.info("{}", result);
        return result;
    }
}
